package com.xszx.dao.impl;

import com.xszx.bean.Order;
import com.xszx.bean.User;
import com.xszx.bean.dto.OrderDto;
import com.xszx.dao.OrderDao;
import com.xszx.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class OrderDaoImpl implements OrderDao {
    @Override
    public List<OrderDto> getOrdersByUserId(Integer userId) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_order where userid = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, userId);
            ResultSet resultSet = ps.executeQuery();

            List<OrderDto> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String orderSeq = resultSet.getString("orderseq");
                int goodsId = resultSet.getInt("goods_id");
                //根据商品id查询图片
                String url = getPicture(goodsId);
                String goodsName = resultSet.getString("goods_name");
                int goodsNum = resultSet.getInt("goods_num");
                Double goodsPrice = resultSet.getDouble("goods_price");
                Date orderTime = resultSet.getDate("ordertime");
                String conName = resultSet.getString("conName");
                String address = resultSet.getString("address");
                int orderState = resultSet.getInt("orderstate");
                OrderDto order = new OrderDto(id, orderSeq, goodsId, goodsName, goodsNum, goodsPrice, orderTime, userId, conName, address, orderState, url);
                list.add(order);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private String getPicture(int goodsId) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select goods_imageurl from t_goods where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, goodsId);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                String url = resultSet.getString("goods_imageurl");
                return url;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Order> getAllOrders() {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_order";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();

            List<Order> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String orderseq = resultSet.getString("orderseq");
                Integer goods_id = resultSet.getInt("goods_id");
                String goods_name = resultSet.getString("goods_name");
                Integer goods_num = resultSet.getInt("goods_num");
                Double goods_price = resultSet.getDouble("goods_price");
                Date orderTime = resultSet.getDate("ordertime");
                Integer userId = resultSet.getInt("userid");
                String conName = resultSet.getString("conName");
                String address = resultSet.getString("address");
                Integer orderstate = resultSet.getInt("orderstate");

                Order order = new Order(id, orderseq, goods_id,  goods_name, goods_num, goods_price, orderTime, userId, conName, address, orderstate);
                list.add(order);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateOrderState(Integer id, Integer orderState) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_order set orderstate = ? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, orderState);
            ps.setInt(2, id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addOrder(Order order) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "insert into t_order values(null, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, order.getOrderSeq());
            ps.setInt(2, order.getGoodsId());
            ps.setString(3, order.getGoodsName());
            ps.setInt(4, order.getGoodsNum());
            ps.setDouble(5, order.getGoodsPrice());
            ps.setTimestamp(6, new Timestamp(new java.util.Date().getTime()));
            ps.setInt(7, order.getUserId());
            ps.setString(8, order.getConName());
            ps.setString(9, order.getAddress());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean deleteOrderById(Integer id) {
        try {
            //判断订单是否可以删除
            if (!isDelete(id)) {
                return false;
            }
            Connection connection = JDBCUtil.getConnection();
            String sql = "delete from t_order where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    private boolean isDelete(Integer id) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select orderstate from t_order where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                int orderstate = resultSet.getInt("orderstate");
                if (orderstate == 4) {
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void addOrderAddress(Integer id, String conName, String address) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_order set conName = ?, address = ? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, conName);
            ps.setString(2, address);
            ps.setInt(3, id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
